﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: HYF
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using FriendStar.Application.SystemManage;
using FriendStar.Application.BOM;
using FriendStar.Code;
using FriendStar.Domain.Entity.BOM;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FriendStar.Application.MainData;

namespace FriendStar.Web.Areas.MainData.Controllers
{
    public class MakeLineController : ControllerBase
    {
        ZSJ_B1_GH_SupplierInformationApp zsj_b1_gh_supplierinformationApp = new ZSJ_B1_GH_SupplierInformationApp();
        V_LXK_16_LX_MakeLineApp v_lxk_16_lx_makelineApp = new V_LXK_16_LX_MakeLineApp();
        LXK_16_LX_MakeLineApp lxk_16_lx_makelineApp = new LXK_16_LX_MakeLineApp();

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMakeLineJson(Pagination pagination, string MakeLineNO, string MakeLineName, string MakeExplain)
        {
            var data = new
            {
                rows = v_lxk_16_lx_makelineApp.GetList(pagination, MakeLineNO, MakeLineName, MakeExplain),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMakeLineSortJson(string sidx, string sord, int rows, string MakeLineNO, string MakeLineName, string MakeExplain, string currNumber)
        {
            Pagination pagination = new Pagination();
            pagination.rows = int.MaxValue;
            pagination.page = 1;
            pagination.sidx = sidx;
            pagination.sord = sord;
            var list = v_lxk_16_lx_makelineApp.GetList(pagination, MakeLineNO, MakeLineName, MakeExplain);
            int index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].MakeLineID == currNumber)
                {
                    index = i + 1;
                    break;
                }
            }
            if (index > 0)
            {
                var data = new
                {
                    currIndex = index % rows,
                    currPage = (index % rows == 0) ? (index / rows) : (index / rows) + 1
                };
                return Content(data.ToJson());
            }
            else
            {
                var data = new
                {
                    currIndex = 1,
                    currPage = 1
                };
                return Content(data.ToJson());
            }
        }
        [HttpGet]
        [HandlerAuthorize]
        public ActionResult MakeLineInput(string status, string number)
        {
            ViewBag.status = (status != "U") ? "I" : status;
            ViewBag.number = (number == null) ? "" : number;
            ViewBag.firstCompanyNo = zsj_b1_gh_supplierinformationApp.GetList("IsInsideUnit = '1' AND IsUnit = '1'");
            ViewBag.secondCompanyNo = zsj_b1_gh_supplierinformationApp.GetList("IsProduct ='1'   or IsCustomer='1'");
            if (status == "U")
            {
                ViewBag.oldEntity = lxk_16_lx_makelineApp.GetForm(number).ToJson();
            }
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitMakeLineForm(string status, LXK_16_LX_MakeLineEntity entity, string keyValue)
        {
            if (status == "I")
            {
                if (lxk_16_lx_makelineApp.CheckedEntity(entity))
                {
                    if (lxk_16_lx_makelineApp.CheckSecondCompany(entity))
                    {
                        lxk_16_lx_makelineApp.SubmitForm(entity, "");
                        return Success("新增操作成功。", entity.MakeLineID);
                    }
                    else
                    {
                        return Error("新增失败，当前二级单位已经存在路线编号。");
                    }
                }
                else
                {
                    return Error("新增失败，已经存在相同路线编号数据。");
                }
            }
            else if (status == "U")
            {
                if (lxk_16_lx_makelineApp.CheckedEntity(entity))
                {
                    LXK_16_LX_MakeLineEntity oldEntity = lxk_16_lx_makelineApp.GetForm(entity.MakeLineID);
                    if (oldEntity != null)
                    {
                        lxk_16_lx_makelineApp.SubmitForm(entity, entity.MakeLineID);
                        return Success("修改操作成功。", entity.MakeLineID);
                    }
                    else
                    {
                        return Error("修改失败，没找到待修改的路线数据。");
                    }
                }
                else
                {
                    return Error("修改失败，已经存在相同路线编号数据。");
                }
            }
            else if (status == "D")
            {

                LXK_16_LX_MakeLineEntity oldEntity = lxk_16_lx_makelineApp.GetForm(keyValue);
                if (oldEntity != null)
                {
                    lxk_16_lx_makelineApp.DeleteForm(oldEntity.MakeLineID);

                    return Success("删除操作成功。");
                }
                else
                {
                    return Error("删除失败，没找到待删除的路线数据。");
                }
            }
            else
            {
                return Error("不可识别的操作类型。");
            }
        }
    }
}
